#include "alttiledmapeditor.h"

#include "altgraphicszoomwidget.h"
#include "altmaterialwidget.h"
#include "alttiledmap.h"
#include "alttileset.h"
#include "alttileseteditor.h"
#include "alttilesetgraphicsitem.h"

#include <QDockWidget>
#include <QFormLayout>
#include <QGraphicsView>
#include <QLabel>
#include <QPushButton>
#include <QSpinBox>
#include <QVBoxLayout>

namespace Alt {
     TiledMapEditor::TiledMapEditor(const QString& fileName, QWidget* parent) : AbstractMapEditor(parent) {
          m_map = (fileName != "") ? static_cast<TiledMap*>(Map::fromFile(fileName)) : new TiledMap();
          init();
     }

     QString TiledMapEditor::defaultFileFilter() const {
          return tr("Alt tiled map (*.atm)");
     }

     Map* TiledMapEditor::map() const {
          return m_map;
     }

     //private
     void TiledMapEditor::init() {
          m_graphicsView = new QGraphicsView(new QGraphicsScene(this), this);
          m_graphicsView->scene()->addItem(m_map);
          setCentralWidget(m_graphicsView);

          //Property dock
          m_propertyDock = new QDockWidget(tr("Map properties"), this);

          QSpinBox* mapWidthEdit = new QSpinBox(this);

          QSpinBox* mapHeightEdit = new QSpinBox(this);

          QSpinBox* defaultTileNumberEdit = new QSpinBox(this);

          QSpinBox* defaultMaterialEdit = new QSpinBox(this);

          QWidget* propertyWidget = new QWidget(m_propertyDock);
          QFormLayout* propertyLayout = new QFormLayout(propertyWidget);
          propertyLayout->addRow(tr("Map width:"), mapWidthEdit);
          propertyLayout->addRow(tr("Map height:"), mapHeightEdit);
          propertyLayout->addRow(tr("Default tile number:"), defaultTileNumberEdit);
          propertyLayout->addRow(tr("Default material ID:"), defaultMaterialEdit);
          m_propertyDock->setWidget(propertyWidget);

          //Tile dock
          m_tileDock = new QDockWidget(tr("Tile properties"), this);

          QSpinBox* tileNumberEdit = new QSpinBox(this);

          QSpinBox* materialEdit = new QSpinBox(this);

          QWidget* tileWidget = new QWidget(m_tileDock);
          QFormLayout* tileLayout = new QFormLayout(tileWidget);
          tileLayout->addRow(tr("Tile number:"), tileNumberEdit);
          tileLayout->addRow(tr("Material ID:"), materialEdit);
          m_tileDock->setWidget(tileWidget);

          //Tileset dock
          m_tilesetDock = new QDockWidget(tr("Tileset"), this);

          QGraphicsView* tilesetView = new QGraphicsView(new QGraphicsScene(this), this);
          tilesetView->scene()->addItem(new TilesetGraphicsItem(&m_map->tileset()));

          GraphicsZoomWidget* tilesetZoom = new GraphicsZoomWidget(tilesetView, this);

          QLabel* tileNumberLabel = new QLabel(tr("Selected tile number:"), this);

          QSpinBox* tileNumberSpin = new QSpinBox(this);

          QPushButton* tilesetEditButton = new QPushButton(tr("Edit tileset"), this);

          QWidget* tilesetWidget = new QWidget(m_tilesetDock);
          QVBoxLayout* tilesetLayout = new QVBoxLayout(tilesetWidget);
          tilesetLayout->addWidget(tilesetView);
          tilesetLayout->addWidget(tilesetZoom);
          tilesetLayout->addWidget(tileNumberLabel);
          tilesetLayout->addWidget(tileNumberSpin);
          tilesetLayout->addWidget(tilesetEditButton);
          m_tilesetDock->setWidget(tilesetWidget);

          //Material dock
          m_materialDock = new QDockWidget(tr("Materials"), this);
          m_materialDock->setWidget(new MaterialWidget(true, this));

          //Rendering dock
          m_renderDock = new QDockWidget(tr("Rendering"), this);
          GraphicsZoomWidget* zoomWidget = new GraphicsZoomWidget(m_graphicsView, m_renderDock);
          m_renderDock->setWidget(zoomWidget);

          addDockWidget(Qt::LeftDockWidgetArea, m_propertyDock);
          addDockWidget(Qt::LeftDockWidgetArea, m_tileDock);
          addDockWidget(Qt::RightDockWidgetArea, m_tilesetDock);
          addDockWidget(Qt::RightDockWidgetArea, m_materialDock);
          addDockWidget(Qt::BottomDockWidgetArea, m_renderDock);
          initMenus();
          return;
     }

     void TiledMapEditor::initMenus() {
          return;
     }
}
